﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer.App1AppHandler.Spells.Interfaces;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_BreakResist : Buff, IInstantCast
    {
        private int ResistAdded = 20;
        private int PrismaticResistAdded = 10;

        public Spell_BreakResist()
        {
            minutesLeft = 10;
        }

        public override bool hasCustomTarget(SpellIcon sIcon)
        {
            return true;
        }

        private CreatureBase choosenTarget = null;
        public override bool hasValidTarget(SpellIcon sIcon, Creature creature, List<System.Drawing.Point> hiddenList)
        {
            List<CreatureBase> targetCreatures = new List<CreatureBase>();

            foreach (MapItem mapitem in creature.currentMapItem.mapItemsWithin4)
            {
                if (CreatureAI.isHidden(mapitem.MapX, mapitem.MapY, hiddenList) == false)
                {
                    foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                    {
                        if (creatureBase.HP > 0 && 
                            (creatureBase.Faction != creature.Faction || creature.hateManager.GetCreatureHateInfo(creatureBase) != null) && 
                            creature.buffManager.GetBuffByStackType(sIcon.StackType) == null && 
                            creatureBase.CanSeeMe(creature))

                            targetCreatures.Add(creatureBase);
                    }
                }
            }

            if (targetCreatures.Count > 0)
            {
                targetCreatures = creature.hateManager.prioritizeList(targetCreatures);
                choosenTarget = targetCreatures[0];

                return true;
            }

            return false;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            ResistAdded = SkillManager.AdjustForCombat(castLevel / 2, ResistAdded, CastingCreatureWis / 2);
            PrismaticResistAdded = SkillManager.AdjustForCombat(castLevel / 2, PrismaticResistAdded, CastingCreatureWis / 3);

            ResistAdded = ResistAdded * -1;
            PrismaticResistAdded = PrismaticResistAdded * -1;

            if (spellIcon.Skill == "Fire")
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + ResistAdded;
            else if (spellIcon.Skill == "Ice")
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + ResistAdded;
            else if (spellIcon.Skill == "Holy")
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + ResistAdded;
            else if (spellIcon.Skill == "Unholy")
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + ResistAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + ResistAdded;
            else if (spellIcon.Skill == "Resilience")
            {
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + PrismaticResistAdded;
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + PrismaticResistAdded;
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + PrismaticResistAdded;
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + PrismaticResistAdded;
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + PrismaticResistAdded;
            }
            TargetCreatureBase.Update_Character();

            spellTimer.Start();
        }
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            if (choosenTarget != null)
                CreatureGUID = choosenTarget.CreatureID;

            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            ResistAdded = SkillManager.AdjustForCombat(CastLevel / 2, ResistAdded, CastingCreatureWis / 2);
            PrismaticResistAdded = SkillManager.AdjustForCombat(CastLevel / 2, PrismaticResistAdded, CastingCreatureWis / 3);

            ResistAdded = ResistAdded * -1;
            PrismaticResistAdded = PrismaticResistAdded * -1;

            if (spellIcon.Skill == "Fire")
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + ResistAdded;
            else if (spellIcon.Skill == "Ice")
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + ResistAdded;
            else if (spellIcon.Skill == "Holy")
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + ResistAdded;
            else if (spellIcon.Skill == "Unholy")
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + ResistAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + ResistAdded;
            else if (spellIcon.Skill == "Resilience")
            {
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + PrismaticResistAdded;
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + PrismaticResistAdded;
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + PrismaticResistAdded;
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + PrismaticResistAdded;
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + PrismaticResistAdded;
            }

            TargetCreatureBase.buffManager.UpdateBuffs();
            TargetCreatureBase.Update_Character();

            
            foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin8)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
            }

            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            if (spellIcon.Skill == "Fire")
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire - ResistAdded;
            else if (spellIcon.Skill == "Ice")
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce - ResistAdded;
            else if (spellIcon.Skill == "Holy")
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly - ResistAdded;
            else if (spellIcon.Skill == "Unholy")
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy - ResistAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic - ResistAdded;
            else if (spellIcon.Skill == "Resilience")
            {
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire - PrismaticResistAdded;
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce - PrismaticResistAdded;
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly - PrismaticResistAdded;
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy - PrismaticResistAdded;
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic - PrismaticResistAdded;
            }

            TargetCreatureBase.Update_Character();
        }



        public void InstantCast(CreatureBase castingCreature, CreatureBase targetCreatureBase, SpellIcon _spellIcon, int castLevel)
        {
            if (InitializeInstantCast(castingCreature, targetCreatureBase, _spellIcon, castLevel, true) == false)
                return;

            ResistAdded = SkillManager.AdjustForCombat(CastingCreatureLevel / 2, ResistAdded, CastingCreatureWis / 2);
            PrismaticResistAdded = SkillManager.AdjustForCombat(CastingCreatureLevel / 2, PrismaticResistAdded, CastingCreatureWis / 3);

            ResistAdded = ResistAdded * -1;
            PrismaticResistAdded = PrismaticResistAdded * -1;

            if (spellIcon.Skill == "Fire")
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + ResistAdded;
            else if (spellIcon.Skill == "Ice")
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + ResistAdded;
            else if (spellIcon.Skill == "Holy")
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + ResistAdded;
            else if (spellIcon.Skill == "Unholy")
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + ResistAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + ResistAdded;
            else if (spellIcon.Skill == "Resilience")
            {
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + PrismaticResistAdded;
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + PrismaticResistAdded;
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + PrismaticResistAdded;
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + PrismaticResistAdded;
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + PrismaticResistAdded;
            }

            TargetCreatureBase.buffManager.UpdateBuffs();
            TargetCreatureBase.Update_Character();

            foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin8)
            {
                foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                {
                    if (creatureBase is Character)
                    {
                        Character character = (Character)creatureBase;
                        
                        if (Math.Abs(mapitem.MapX - targetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - targetCreatureBase.MapY) <= 4)
                            character.AddCreatureBase(targetCreatureBase);

                        character.PlaySound("BuffCast", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                    }
                }
            }

            spellTimer.Start();
        }
    }
}
